import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { resolve } from "path";

export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), "");
  return {
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        resolvers: [ElementPlusResolver({ importStyle: "sass" })],
      }),
      Components({
        resolvers: [ElementPlusResolver({ importStyle: "sass" })],
      }),
    ],

    transpileDependencies: true,
    publicDir: "",
    resolve: {
      alias: {
        "@": resolve(__dirname, "src"),
        // extensions: [".js", ".json", ".ts"],
        // 使用路径别名时想要省略的后缀名，可以自己 增减
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import '@/style.scss';`,
        },
      },
    },

    build: {
      // 浏览器兼容性  “esnext | modules”
      target: "modules",
      outDir: "dist",
      //构建后是否生成 source map 文件
      sourcemap: false,
      // chunk 大小警告的限制
      chunkSizeWarningLimit: 500,
    },
    server: {
      port: 8080,

      proxy: {
        // 选项写法
        "/api": {
          target: "http://localhost:3000/api",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, "/"),
        },
      },
    },
    rollupOptions: {
      output: {
        // 分解大块js,
        manualChunks(id) {
          if (id.includes("node_modules")) {
            return id
              .toString()
              .split("node_modules/")[1]
              .split("/")[0]
              .toString();
          }
        },
      },
    },
  };
});
